﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using dis.sys.hw1.ClientProxy;
using dis.sys.hw1.Model;

namespace WpfClient
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private static HotelSearchProxy _service = null;
        private const int SearchServiceAddressArg = 1;
        public ObservableCollection<Hotel> Hotels { get { return _hotels; } }  
        private ObservableCollection<Hotel> _hotels = new ObservableCollection<Hotel>();

        public ObservableCollection<Reservation> Reservations { get { return _reservations; } }
        private ObservableCollection<Reservation> _reservations = new ObservableCollection<Reservation>(); 

        public MainWindow()
        {
            InitializeComponent();
            try
            {
                _service = new HotelSearchProxy(Environment.GetCommandLineArgs()[SearchServiceAddressArg]);
                Status.Background = new SolidColorBrush(Colors.Green);
            }
            catch (Exception)
            {
                Status.Background = new SolidColorBrush(Colors.Red);
                MessageBox.Show("Could not connect to search server.");
            }
        }

        private void BookRoom(object sender, RoutedEventArgs e)
        {
            if (CheckIn.SelectedDate != null && CheckOut.SelectedDate != null)
            {
                int bookingId = _service.Book(CheckIn.SelectedDate.Value.Ticks,
                                              CheckOut.SelectedDate.Value.Ticks, (int) (sender as Button).Tag,
                                              (HotelsData.SelectedItem as Hotel).Name);
                Reservations.Add(new Reservation()
                    {
                        BookingId = bookingId,
                        HotelName = (HotelsData.SelectedItem as Hotel).Name,
                        RoomNumber = (int) (sender as Button).Tag,
                        FromDate = CheckIn.SelectedDate.Value,
                        ToDate = CheckOut.SelectedDate.Value
                    });
                SearchHotels((HotelsData.SelectedItem as Hotel).Name);
            }
            else
            {
                MessageBox.Show("Please choose both check in and check out dates");
            }
        }

        private void SearchClick(object sender, RoutedEventArgs e)
        {
            if (CheckIn.SelectedDate != null && CheckOut.SelectedDate != null)
            {
                SearchHotels();
            }
            else
            {
                MessageBox.Show("Please choose both check in and check out dates");
            }
        }

        private void SearchHotels(string selected = null)
        {
            _hotels.Clear();
            foreach (var hotel in _service.Search(CheckIn.SelectedDate.Value.Ticks,
                                                         CheckOut.SelectedDate.Value.Ticks, Stars.Value,
                                                         Rating.Value))
            {
                _hotels.Add(hotel);
            }

            if(selected != null)
            {
                HotelsData.SelectedItem = _hotels.FirstOrDefault(x => x.Name.Equals(selected));
            }
        }
    }
}
